/**
  * @(#)LogbackManager.java
  *
  * Log file manager for the Logback implementation.
  *
  * @author Mike Reinhold
  * 
  * @license GNU General Public License version 3 (GPLv3)
  *
  * This file is part of Course Scheduler, an open source, cross platform
  * course scheduling tool, configurable for most universities.
  *
  * Copyright (C) 2010 Mike Reinhold
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * 
  */
package com.pollicitus.scheduler.logging.logback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pollicitus.scheduler.core.NamespaceSettings;
import com.pollicitus.scheduler.core.Settings;
import com.pollicitus.scheduler.logging.LogManager;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;


/**
 * Log file manager for the Logback implementation.
 * 
 * @author Mike Reinhold
 *
 */
public class LogbackManager extends LogManager {
	
	//private logback logging property names
	private static final String LOGBACK_NAMESPACES = "logback.namespaces";
	private static final String LOGBACK_LOG_PROPERTIES = "logback.properties";
	private static final String LOGBACK_CONFIGURATION = "logback.configuration";
		
	//instance variables
	protected LoggerContext context;
	protected NamespaceSettings logSettings;
	
	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#initialize(com.scheduler.core.Settings)
	 */
	@Override
	public void initialize(Settings settings) {
		//get the logging namespace settings
		logSettings = new NamespaceSettings(settings, namespace);
		
		//retrieve the logging context and reset it
		context = (LoggerContext)LoggerFactory.getILoggerFactory();
		context.reset();

		//get the logging context properties
		addPropertiesToContext(logSettings);
		
		//use the configurator to make the configuration active
		JoranConfigurator jc = new JoranConfigurator();
		jc.setContext(context);
		
		//apply the configuration and watch for errors
		try{
			jc.doConfigure(logSettings.getProperty(LOGBACK_CONFIGURATION));
		}catch(JoranException e){
			//status printing is handled by the StatusPrinter line below in the finally block
		}finally{
			StatusPrinter.printInCaseOfErrorsOrWarnings(context);
		}
	}

	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#finalize(com.scheduler.core.Settings)
	 */
	@Override
	public void finalize(Settings settings) {
		// TODO METHOD STUB

	}

	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#getLogger(java.lang.String)
	 */
	@Override
	public Logger getLogger(String loggingNamespace) {
		return context.getLogger(loggingNamespace);
	}
	
	/**
	 * Read the Logback configuration file and generate the context properties
	 * as required by the logback xml file. 
	 * 
	 * @param settings the settings required by this log manager
	 */
	private void addPropertiesToContext(Settings settings){
		//retrieve each logger configuration
		for(String logName: settings.getPropertyArray(LOGBACK_NAMESPACES)){
			
			//add the logging properties for this log namespace
			for(String extension: settings.getPropertyArray(LOGBACK_LOG_PROPERTIES)){
				context.putProperty(logName + extension, settings.getProperty(logName + extension));
			}
		}
	}
}
